package com.biz.storage.config;

import com.aliyun.oss.*;
import org.apache.commons.lang.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.annotation.*;

/**
 * @Description
 *    阿里云配置
 * @Author liuxi
 * @Date 2024/4/27
 **/
@Configuration
@ConditionalOnProperty(name = "storage.service.type", havingValue = "ALIYUN_OSS")
public class AliyunOssConfig {

    /**
     *端点
     */
    @Value("${storage.aliyun.oss.end-point}")
    private String endPoint;

    /**
     * bucket 名称
     */
    @Value("${storage.aliyun.oss.bucket-name}")
    private String bucketName;

    /**
     *id
     */
    @Value("${storage.aliyun.oss.access-key-id}")
    private String accessKeyId;

    /**
     * 密钥
     */
    @Value("${storage.aliyun.oss.access-key-secret}")
    private String accessKeySecret;

    @Bean
    public OSS ossClient() {
        if (StringUtils.isBlank(this.endPoint)) {
            throw new RuntimeException("[end-point] cannot be empty!!");
        }
        if (StringUtils.isBlank(this.bucketName)) {
            throw new RuntimeException("[bucket-name] cannot be empty!!");
        }
        if (StringUtils.isBlank(this.accessKeyId)) {
            throw new RuntimeException("[access-key-id] cannot be empty!!");
        }
        if (StringUtils.isBlank(this.accessKeySecret)) {
            throw new RuntimeException("[access-key-secret] cannot be empty!!");
        }
        return new OSSClientBuilder().build(this.endPoint, this.accessKeyId, this.accessKeySecret);
    }
}
